package doa;

import java.util.ArrayList;
import java.util.List;
import models.Person;
import org.hibernate.Session;
import org.hibernate.Transaction;
import service.HibernateUtil;

/**
 *
 * @author User
 */
public class PersonDAO {

    public PersonDAO() {
    }
    
    
    public static List<Person> getAllPersons() {

        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<Person> person_list = session.createCriteria(Person.class).list();
        tx.commit();

        return person_list;
    }
    
    
    public static ArrayList insertPerson(Person person) {
        System.out.println("before");
        ArrayList validation_errors = new ArrayList();
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        // product = getProductFromRequest(request);
        
        session.save(person);
        tx.commit();
        System.out.println("saved");
        return validation_errors;
    }
//    private static Person getNewsFromRequest(HttpServletRequest request, Course course) {
//        Person p = new Person();
//
//        if (request.getParameter("newsid") != null && !request.getParameter("newsid").isEmpty()) {
//            p.setNewsid(Integer.parseInt(request.getParameter("newsid")));
//        }
//        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
//        Date date = new Date();
//        p.setNewsdate(dateFormat.format(date));
//        if (request.getParameter("newstitle") != null && !request.getParameter("newstitle").isEmpty()) {
//            p.setNewstitle(request.getParameter("newstitle"));
//        }
//        if (request.getParameter("newscontent") != null && !request.getParameter("newscontent").isEmpty()) {
//            p.setNewscontent(request.getParameter("newscontent"));
//        }
//        if (request.getParameter("newsglobal") != null && !request.getParameter("newsglobal").isEmpty()) {
//            p.setNewsglobal(Integer.parseInt(request.getParameter("newsglobal")));
//        }
//        if (course != null) {
//            n.setCourse(course);
//        }
//        return n;
//    }
    
    
    
}
